

def main():
	user = login()
	if user == "error":
		print("多次密码输入错误,登录失败")
		return
	print("\033[2J\033[1;1H", end="")

	if user == "user":
		user_menu()
	
def admin_menu():
	print("1.添加账务  2.编辑账务  3.删除账务  4.查询账务  5.退出系统")
	db = SimulationDatabase()
	while True:
		choice = input("请输入操作的序号[1-5]: ")
		if choice == "1":
			print("添加账务")
			data = {}
			data["id"] = -1
			data["type"] = input("请输入账务类型: ")
			data["account"] = input("请输入账户: ")
			data["money"] = float(input("请输入金额: "))
			data["time"] = input("请输入时间: ")
			data["note"] = input("请输入备注: ")
			db.insert_data(data)
			print("添加账务成功!")
		elif choice == "2":
			data = {}
			data["id"] = int(input("请输入账务ID: "))
			str = input("请输入修改内容(多个条件用","分割格式: key=value, 例如: 类别=吃饭支出): ")
			for item in str.split(","):
				key, value = item.split("=")
				key = switch_key(key)
				data[key] = value.strip()
				if key == "money":
					data[key] = float(data[key])
				if key == "id":
					data[key] = int(data[key])
			print("编辑账务成功")
		elif choice == "3":
			id = int(input("请输入账务ID: "))
			db.delete_data(id)
			print("删除账务成功")
		elif choice == "4":
			ata = db.select_all()
			print_bill(data)
			print("查询成功")
		elif choice.lower() == "q":
			print("退出系统!")
			break
		else:
			print("无效选择!")


def user_menu():
	db = SimulationDatabase()
	print("1.查询所有  2.按条件查询")
	while True:
		choice = input("请输入操作的序号[1-2]: ")
		if choice == "1":
			data = db.select_all()
			print_bill(data)
			print("查询成功")

		elif choice == "2":
			print("按条件查询")
			str = input("请输入查询条件(多个条件用\",\"分割格式: key=value, 例如: 类别=吃饭支出): ")
			condition = {}
			for item in str.split(","):
				key, value = item.split("=")
				key = switch_key(key)
				if key == None:
					print(f"无效条件: {key}")
					continue
				condition[key] = value.strip()
				if key == "money":
					condition[key] = float(condition[key])
				if key == "id":
					condition[key] = int(condition[key])
			data = db.select_by_condition(condition)
			print_bill(data)	
			print("查询成功")

		elif choice.lower() == "q":
			print("退出系统!")
			break
		else:
			print("无效选择!")

def switch_key(data):
	match data:
		case "ID":
			return "id"
		case "id":
			return "id"
		case "类型":
			return "type"
		case "账户":
			return "account"
		case "金额":
			return "money"
		case "时间":
			return "time"
		case "备注":
			return "note"
		case _:
			return None
	
	
def print_bill(data):
	print(f"{'ID':<4}{'类型':<20}{'账户':<20}{'金额':<12}{'时间':<12}{'备注':<30}")
	for item in data:
		print(f"{item['id']:<4}{item['type']:<20}{item['account']:<20}{item['money']:<12}{item['time']:<12}{item['note']:<30}")

class SimulationDatabase:
	def __init__(self):
		self.index = 10
		self.data = [
			{"id": 1, "type":"吃饭支出","account":"交通银行","money":100.0,"time":"2023-10-01","note":"吃饭"},
			{"id": 2, "type":"交通出行","account":"现金","money":50.0,"time":"2023-10-02","note":"打车"},
			{"id": 3, "type":"购物消费","account":"招商银行","money":200.0,"time":"2023-10-03","note":"买衣服"},
			{"id": 4, "type":"娱乐消费","account":"建设银行","money":150.0,"time":"2023-10-04","note":"看电影"},
			{"id": 5, "type":"其他支出","account":"工商银行","money":300.0,"time":"2023-10-05","note":"其他"},
			{"id": 6, "type":"吃饭支出","account":"农业银行","money":80.0,"time":"2023-10-06","note":"吃饭"},
			{"id": 7, "type":"交通出行","account":"中国银行","money":120.0,"time":"2023-10-07","note":"打车"},
			{"id": 8, "type":"购物消费","account":"邮政储蓄银行","money":250.0,"time":"2023-10-08","note":"买衣服"},
			{"id": 9, "type":"娱乐消费","account":"浦发银行","money":180.0,"time":"2023-10-09","note":"看电影"},
			{"id": 10, "type":"其他支出","account":"平安银行","money":400.0,"time":"2023-10-10","note":"其他"}
		]
	def select_all(self):
		return self.data
	
	def select_by_condition(self, condition:dict):
		result = []
		for item in self.data:
			if all(item[key] == value for key, value in condition.items()):
				result.append(item)
		return result
	
	def update_data(self, data):
		for key in list(data.keys()):
			if key not in ["id", "type", "account", "money", "time", "note"]:
				del data[key]
		for i, item in self.data:
			if item["id"] == data["id"]:
				self.data[i].update(data)
				return True
		return False
		
	def insert_data(self, data):
		data["id"] = self.index+1
		self.index += 1
		self.data.append(data)
		return True
	
	def delete_data(self, id):
		for i, item in self.data:
			if item["id"] == id:
				del self.data[i]
				return True
		return False

def login():
	for i in range(3):
		username = input("请输入用户名: ")
		password = input("请输入密码: ")
		if username == "admin" and password == "123456":
			return "root"
		elif username == "lisi" and password == "123456":
			return "user"
		else :
			print("用户名或密码错误,请重新输入")
	
	return "error"



main()